﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using cpe.cpein.DATA;

namespace cpe.cpein.BUSINESS
{
    public static class BUArea
    {
        /// <summary>
        /// Get All the Area of the database.
        /// </summary>
        /// <param name="searchInDeleted"></param>
        /// <returns></returns>
        public static List<Area> GetAllArea(bool searchInDeleted)
        {
            List<Area> ares = new List<Area>();
            using (cpeinEntities db = new cpeinEntities())
            {
                var results = from Area in db.Area
                              where Area.are_isdeleted == searchInDeleted
                              select Area;
                foreach (Area tmp in results)
                    ares.Add(tmp);
            }

            return ares;
        }

        /// <summary>
        /// Get the Area by id.
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public static Area GetAreaByID(int id)
        {
            if (id <= 0)
                return null;
            else
            {
                using (cpeinEntities db = new cpeinEntities())
                {
                    var are = from Area in db.Area
                              where Area.are_id == id
                              select Area;
                    foreach (Area tmp in are)
                        return tmp;
                    return null;
                }
            }
        }

        /// <summary>
        /// are a Area into the database.
        /// </summary>
        /// <param name="are"></param>
        /// <returns></returns>
        public static Area areArea(Area are)
        {
            if (are != null)
            {
                using (cpeinEntities db = new cpeinEntities())
                {
                    Area nare = db.Area.Add(are);
                    db.SaveChanges();
                    return nare;
                }
            }
            else
                return null;
        }

        /// <summary>
        /// Remove a specific Area of the database.
        /// </summary>
        /// <param name="are"></param>
        /// <returns></returns>
        public static bool RemoveArea(Area are)
        {
            if (are != null)
            {
                are.are_isdeleted = true;
                if (UpdateArea(are) != null)
                {
                    using (cpeinEntities db = new cpeinEntities())
                    {
                        db.SaveChanges();
                        return true;
                    }
                }
            }

            return false;
        }

        /// <summary>
        /// Update the Area into the database.
        /// </summary>
        /// <param name="are"></param>
        /// <returns></returns>
        public static Area UpdateArea(Area are)
        {
            if (are != null)
            {
                using (cpeinEntities db = new cpeinEntities())
                {
                    var current = from Area in db.Area
                                  where Area.are_id == are.are_id
                                  select Area;

                    foreach (var tmp in current)
                    {
                        tmp.are_label = are.are_label;
                        tmp.are_isdeleted = are.are_isdeleted;

                        db.SaveChanges();
                    }

                    are = current as Area;
                }
            }

            return are;
        }
    }
}
